/*
 * @Author: your name
 * @Date: 2021-09-18 09:48:27
 * @LastEditTime: 2022-10-19 10:15:29
 * @LastEditors: zshawk1982
 * @Description: In User Settings Edit
 * @FilePath: \media_web_client\src\router\index.js
 */
import { createRouter, createWebHashHistory,createWebHistory } from "vue-router";
import Home from "../views/Home.vue";


const routes = [
  {
    path: "/",
    redirect: "/dashboard",
    meta: {
      requireAuth:true,
    },
  },
  {
    path: "/",
    name: "Home",
    component: Home,
    children: [
      {
        path: "/dashboard",
        name: "dashboard",
        meta: {
          requireAuth:true,
          title: "系统首页",
        },
        component: () =>
          import(
            /* webpackChunkName: "dashboard" */ "../views/dashboard/Dashboard.vue"
          ),
      },
      {
        path: "/userTable",
        name: "userTable",
        meta: {
          requireAuth:true,
          title: "用户信息",
        },
        component: () =>
          import(
            /* webpackChunkName: "user" */ "../views/userTable/userTable.vue"
          ),
      },
      {
        path: "/userAdd",
        name: "userAdd",
        meta: {
          requireAuth:true,
          title: "新增用户",
        },
        component: () =>
          import(
            /* webpackChunkName: "user" */ "../views/userTable/userAdd.vue"
          ),
      },
      {
        path: "/userCenter",
        name: "userCenter",
        meta: {
          requireAuth:true,
          title: "修改密码",
        },
        component: () =>
          import(
            /* webpackChunkName: "user" */ "../views/userCenter/userCenter.vue"
          ),
      },
      {
        path: "/foodTable",
        name: "foodTable",
        meta: {
          requireAuth:true,
          title: "食物信息",
        },
        component: () =>
          import(
            /* webpackChunkName: "food" */ "../views/foodTable/foodTable.vue"
          ),
      },
      {
        path: "/foodAdd",
        name: "foodAdd",
        meta: {
          requireAuth:true,
          title: "新增食物",
        },
        component: () =>
          import(
            /* webpackChunkName: "food" */ "../views/foodTable/foodAdd.vue"
          ),
      },
      {
        path: "/foodAddAll",
        name: "foodAddAll",
        meta: {
          requireAuth:true,
          title: "批量导入食物",
        },
        component: () =>
          import(
            /* webpackChunkName: "food" */ "../views/foodTable/foodAddAll.vue"
          ),
      },
      {
        path: "/foodEdit",
        name: "foodEdit",
        meta: {
          requireAuth:true,
          title: "编辑食物",
        },
        component: () =>
          import(
            /* webpackChunkName: "food" */ "../views/foodTable/foodEdit.vue"
          ),
      },
      {
        path: "/knowledge",
        name: "knowledge",
        meta: {
          requireAuth:true,
          title: "知识表单",
        },
        component: () =>
          import(
            /* webpackChunkName: "knowledge" */ "../views/knowledge/knowledge.vue"
          ),
      },
      {
        path: "/knowledgeAdd",
        name: "knowledgeAdd",
        meta: {
          requireAuth:true,
          title: "新增知识",
        },
        component: () =>
          import(
            /* webpackChunkName: "knowledge" */ "../views/knowledge/knowledgeAdd.vue"
          ),
      },
      {
        path: "/knowledgeEdit",
        name: "knowledgeEdit",
        meta: {
          requireAuth:true,
          title: "编辑知识",
        },
        component: () =>
          import(
            /* webpackChunkName: "knowledge" */ "../views/knowledge/knowledgeEdit.vue"
          ),
      },
      {
        path: "/recipeTable",
        name: "recipeTable",
        meta: {
          requireAuth:true,
          title: "食谱信息",
        },
        component: () =>
          import(
            /* webpackChunkName: "recipe" */ "../views/recipe/recipeTable.vue"
          ),
      },
      {
        path: "/recipeAdd",
        name: "recipeAdd",
        meta: {
          requireAuth:true,
          title: "新增食谱",
        },
        component: () =>
          import(
            /* webpackChunkName: "recipe" */ "../views/recipe/recipeAdd.vue"
          ),
      },
      {
        path: "/recipeRecommand",
        name: "recipeRecommand",
        meta: {
          requireAuth:true,
          title: "设置推荐食谱",
        },
        component: () =>
          import(
            /* webpackChunkName: "recipe" */ "../views/recipe/recipeRecommand.vue"
          ),
      },
      {
        path: "/recipeEdit",
        name: "recipeEdit",
        meta: {
          requireAuth:true,
          title: "编辑食谱",
        },
        component: () =>
          import(
            /* webpackChunkName: "recipe" */ "../views/recipe/recipeEdit.vue"
          ),
      },
      {
        path: '/bloodGlucose',
        name: 'bloodGlucose',
        meta: {
          requireAuth:true,
          title: '血糖信息',
        },
        component: () =>
          import(
            /* webpackChunkName: "bloodGlucose" */ '../views/bloodGlucoseManagement/bloodGlucose.vue'
          ),
      },
      {
        path: '/hemoglobin',
        name: 'hemoglobin',
        meta: {
          requireAuth:true,
          title: '糖化血红蛋白信息',
        },
        component: () =>
          import(
            /* webpackChunkName: "hemoglobin" */ '../views/bloodGlucoseManagement/hemoglobin.vue'
          ),
      },
      {
        path: '/dietRecordTable',
        name: 'dietRecordTable',
        meta: {
          requireAuth:true,
          title: '饮食管理',
        },
        component: () =>
          import(
            /* webpackChunkName: "dietRecord" */ '../views/dietRecord/dietRecordTable.vue'
          ),
      }
    ],
  },
  {
    path: "/login",
    name: "Login",
    meta: {
      title: "登录",
    },
    component: () =>
      import(/* webpackChunkName: "login" */ "../views/login/Login.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  // history: createWebHistory(),
  routes,
});
// 路由守卫
 
// to：即将要进入的目标 路由对象
// from：当前导航正要离开的路由
//next：放行
router.beforeEach((to, from, next) => {
  let storedData = localStorage.getItem("userInfo")?JSON.parse(localStorage.getItem("userInfo")):0;
  let currentTime = new Date().getTime();
  if (currentTime > storedData.expiration) {
    // 数据已过期，清除
    localStorage.removeItem('userInfo');
    storedData = null;
  }
  if (to.matched.length != 0) {
      if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
          if (localStorage.getItem("userInfo")) { // 通过浏览器本地缓存判断当前的token是否存在
              next();
          } else {
              next('/Login')
          }
      } else {
        next()
      }
  } else {
      next({
          path: '/login',
          query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
  }
})
export default router;
